import Dao from "../../utils/dao";
import md5 from 'md5'

export async function register(options) {
    var user = new Dao('user')
    var findNameResult = await  user.selectOne({name: options.name})
    var inserResult = undefined
    if (!findNameResult) {
        options.password = md5(options.password)
        inserResult = await user.insert(options)
    } else {
        throw {message: "用户已经存在"}
    }
    return inserResult.toJSON()
}

export async function login(options) {
    var user = new Dao('user')
    var findNameResult = await  user.selectOne({name: options.name, password: md5(options.password)})
    if (!findNameResult) {
        throw {message: "用户不存在/密码错误"}
    } else {
        return findNameResult.toJSON()

    }

}